#! /usr/bin/env bash
# shellcheck source=tool/test/.ctx

ec=0

(
  desc='Top-level directory creation'
  read -r -d '' expect << .
[
  {
    "effect_type": "create",
    "path_name": "s/self/live@d",
    "path_type": "watcher"
  },
  {
    "effect_type": "create",
    "path_name": "d/a",
    "path_type": "dir"
  },
  {
    "effect_type": "create",
    "path_name": "d/b",
    "path_type": "dir"
  },
  {
    "effect_type": "create",
    "path_name": "d/c",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "s/self/die@d",
    "path_type": "watcher"
  }
]
.

  echo -n "$desc ... "

  . "$(dirname "$0")/.ctx"

  actual=$(
    watch-async "$testdir" -ms 500 > "$testdir.json"
    while read -r d
    do mkdir "$d"
    done << .
      a
      b
      c
.
    wait # for the watcher
    show-events "$testdir" | without-effect-time
  )

  check-result "$expect" "$actual"
)
ec=$((ec + $?))

(
  desc='Top-level directory creation, then destruction'
  read -r -d '' expect << .
[
  {
    "effect_type": "create",
    "path_name": "s/self/live@d",
    "path_type": "watcher"
  },
  {
    "effect_type": "create",
    "path_name": "d/a",
    "path_type": "dir"
  },
  {
    "effect_type": "create",
    "path_name": "d/b",
    "path_type": "dir"
  },
  {
    "effect_type": "create",
    "path_name": "d/c",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "d/a",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "d/b",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "d/c",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "s/self/die@d",
    "path_type": "watcher"
  }
]
.

  echo -n "$desc ... "

  . "$(dirname "$0")/.ctx"

  actual=$(
    watch-async "$testdir" -ms 800 > "$testdir.json"
    while read -r d
    do mkdir "$d"
    done << .
      a
      b
      c
.
    [ "$(uname -s)" = Darwin ] && sleep 0.2
    while read -r d
    do rmdir "$d"
    done << .
      a
      b
      c
.
    wait
    show-events "$testdir" | without-effect-time
  )

  check-result "$expect" "$actual"
)
ec=$((ec + $?))

(
  desc='Top-level directory creation, intermixed with destruction'
  read -r -d '' expect << .
[
  {
    "effect_type": "create",
    "path_name": "s/self/live@d",
    "path_type": "watcher"
  },
  {
    "effect_type": "create",
    "path_name": "d/a",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "d/a",
    "path_type": "dir"
  },
  {
    "effect_type": "create",
    "path_name": "d/b",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "d/b",
    "path_type": "dir"
  },
  {
    "effect_type": "create",
    "path_name": "d/c",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "d/c",
    "path_type": "dir"
  },
  {
    "effect_type": "destroy",
    "path_name": "s/self/die@d",
    "path_type": "watcher"
  }
]
.

  echo -n "$desc ... "

  . "$(dirname "$0")/.ctx"

  actual=$(
    watch-async "$testdir" -ms 800 > "$testdir.json"
    while read -r d
    do mkdir "$d"
       rmdir "$d"
    done << .
      a
      b
      c
.
    wait
    show-events "$testdir" | without-effect-time
  )

  check-result "$expect" "$actual"
)
ec=$((ec + $?))

